package vendas.web.action;

import gminet.infra.business.BusinessException;
import gminet.infra.db.TransactionException;
import gminet.infra.web.WebException;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import vendas.business.SellerBusiness;
import vendas.business.SellerTypeBusiness;
import vendas.dto.ActionType;
import vendas.dto.ActiveStatus;
import vendas.dto.Seller;
import vendas.dto.SellerType;

@ManagedBean(name = "sellerAction")
@SessionScoped
public class SellerAction extends WebAction {

    private static final long serialVersionUID = -6562773108260400177L;

    private Seller seller;
    
    private List<Seller> sellers;

    private List<SelectItem> sellerTypesItems;

    public SellerAction() {
        super();
    }

    public String listSellers() throws WebException {

        verifyLogin();
        String pageReturn = "listSellers";
        try {
            setSellers(SellerBusiness.findAll());
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String prepareInsertSeller() throws WebException {

        verifyLogin();
        setActionType(ActionType.INSERT);
        setSeller(null);
        return "editSeller";
    }

    public String insertSeller() throws WebException {

        verifyLogin();
        String pageReturn;
        try {
            // CARREGA INFORMACOES DO SELLER TYPE
            getSeller().setType(SellerType.getSellerTypeById(getSeller().getType().getId()));
            //
            SellerBusiness.insert(seller);
            pageReturn = listSellers();
        } catch (BusinessException be) {
            addBusinessException("frmSeller", be);
            pageReturn = "editSeller";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String prepareEditSeller() throws WebException {

        verifyLogin();
        String pageReturn = "editSeller";
        setActionType(ActionType.EDIT);
        try {
            int sellerId = getSeller().getId();
            setSeller(SellerBusiness.findByPK(sellerId));
        } catch (BusinessException be) {
            addBusinessException("frmListSellers", be);
            pageReturn = listSellers();
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String editSeller() throws WebException {

        verifyLogin();
        String pageReturn;
        try {
            // CARREGA INFORMACOES DO SELLER TYPE
            getSeller().setType(SellerType.getSellerTypeById(getSeller().getType().getId()));
            //
            SellerBusiness.update(seller);
            pageReturn = listSellers();
        } catch (BusinessException be) {
            addBusinessException("frmSeller", be);
            pageReturn = "editSeller";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String updateStatusSeller() throws WebException {

        verifyLogin();
        String pageReturn;
        try {
            ActiveStatus activeStatus = null;
            if (getActionType().equals(ActionType.ACTIVE)) {
                activeStatus = ActiveStatus.ACTIVE;
            } else if (getActionType().equals(ActionType.DESACTIVE)) {
                activeStatus = ActiveStatus.INACTIVE;
            }
            if (activeStatus != null) {
                seller.setActiveStatus(activeStatus);
                SellerBusiness.updateActiveStatus(seller);
            }
            pageReturn = listSellers();
        } catch (BusinessException be) {
            addBusinessException("frmSeller", be);
            pageReturn = "editSeller";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public List<SelectItem> getSellerTypesItems() throws WebException {

        if (this.sellerTypesItems == null) {
            this.sellerTypesItems = new ArrayList<SelectItem>();
            List<SellerType> sellerTypes = SellerTypeBusiness.findAll();
            for (SellerType sellerType : sellerTypes) {
                this.sellerTypesItems.add(new SelectItem(new Integer(sellerType.getId()), sellerType.getName()));
            }
        }
        return this.sellerTypesItems;
    }

    public Seller getSeller() {
        if (this.seller == null) {
            this.seller = new Seller();
        }
        return seller;
    }

    public void setSeller(Seller seller) {
        this.seller = seller;
    }

    public List<Seller> getSellers() {
        return sellers;
    }

    public void setSellers(List<Seller> sellers) {
        this.sellers = sellers;
    }

}
